let express = require('express');
let router = express.Router();

const classesService = require('../service/classesService');
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');

// 此路由需要登录后才能访问
router.use(checkJWTMiddleWare);

/*
 * 获取所有班级信息的接口
 */
router.get('/getAllClassesInfo', async function(req, res, next) {
    let result = await classesService.getAllClassesList();
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查看某个班级的详细信息接口
 */
// router.get('/getAllClassesList', async function(req, res, next) {});

/*
 * 修改班级详细信息接口
 */
router.post('/updateClass', async function(req, res, next) {
    let classId = req.body.classId;
    let className = req.body.className;
    let gradeId = req.body.gradeId;
    let facultyId = req.body.collegeId;
    let level = req.body.level;
    let teacherId = req.body.counselorId;
    let major = req.body.major;
    if (!classId || !className || !gradeId || !facultyId || !level || !teacherId) {
        res.status(400).json({ success: false, msg: "参数不足，修改失败" });
        return ;
    }
    if (className.includes('-')) {
        res.status(400).json({ success: false, msg: "班级名不能包含'-'" });
        return ;
    }
    let result = await classesService.updateClasses(classId, className, gradeId, facultyId, level, teacherId, major);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 删除班级接口
 */
router.post('/delClass', async function(req, res, next) {
    let classId = req.body.classId;
    if (!classId) {
        res.status(400).json({ success: false, msg: "参数不足，操作失败" });
        return ;
    }
    let result = await classesService.delClasses(classId);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 新增班级接口
 */
router.post('/addClass', async function(req, res, next) {
    let className = req.body.className;
    let gradeId = req.body.gradeId;
    let facultyId = req.body.collegeId;
    let level = req.body.level;
    let teacherId = req.body.counselorId;
    let major = req.body.major;
    if (!className || !gradeId || !facultyId || !level || !teacherId) {
        res.status(400).json({ success: false, msg: "参数不足，新增失败" });
        return ;
    }
    if (className.includes('-')) {
        res.status(400).json({ success: false, msg: "班级名不能包含'-'" });
        return ;
    }
    let result = await classesService.addClasses(className, gradeId, facultyId, level, teacherId, major);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 获取所有年级的接口
 */
router.get('/getAllGradeList', async function(req, res, next) {
    let result = await classesService.getAllGradeList();
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;
